package com.kevin.ymqquan.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;

public class ActiveApplyActivity extends Activity {

	private App application;
	private String activeId;
	private ImageView iv_active_apply_back;
	private EditText et_activeapply_mcount;
	private EditText et_activeapply_wcount;
	private EditText et_activeapply_remark;

	private Dialog dialog;
	private Button btn_active_apply_submit;
	private int isJoin=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active_apply);

		Intent i = getIntent();
		activeId = i.getStringExtra("activeId");
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		application = (App) getApplication();
		iv_active_apply_back = (ImageView) findViewById(R.id.iv_active_apply_back);
		et_activeapply_mcount = (EditText) findViewById(R.id.et_activeapply_mcount);
		et_activeapply_wcount = (EditText) findViewById(R.id.et_activeapply_wcount);
		et_activeapply_remark = (EditText) findViewById(R.id.et_activeapply_remark);

		btn_active_apply_submit = (Button) findViewById(R.id.btn_active_apply_submit);

		btn_active_apply_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int mcount ;
				int wcount ;

				try {
					mcount = Integer.parseInt(et_activeapply_mcount.getText()
							.toString());
				} catch (Exception e) {
					mcount = 0;
					e.printStackTrace();

				}

//add
				try {
					wcount = Integer.parseInt(et_activeapply_wcount.getText()
							.toString());
				} catch (Exception e) {
					wcount = 0;
					e.printStackTrace();

				}

//				if (mcount == 0 && wcount == 0) {
//					CustomToast.CustomActivityToast(ActiveApplyActivity.this,
//							"人数不能全为空", 0);
//				} else {
					if (Helper.checkConnection(ActiveApplyActivity.this)) {
						dialog = CustomToast.createLoadingDialog(
								ActiveApplyActivity.this, "提交中...");
						dialog.setCancelable(true);
						dialog.show();
						ActiveApplyReasonAsyTask suggestionTask = new ActiveApplyReasonAsyTask();
						suggestionTask.execute();

					} else {
						CustomToast
								.CustomActivityToast(ActiveApplyActivity.this,
										"网络连接异常，请检查您的网络", -80);
					}
//				}

			}
		});

		iv_active_apply_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// ClubApplyReasonActivity.this.finish();
				ActiveApplyActivity.this.finish();
			}
		});
	}

	class ActiveApplyReasonAsyTask extends AsyncTask<String, Object, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			int mcount;
			int wcount ;

			try {
				mcount = Integer.parseInt(et_activeapply_mcount.getText()
						.toString());
			} catch (Exception e) {
				mcount = 0;
				e.printStackTrace();

			}

			try {
				wcount = Integer.parseInt(et_activeapply_wcount.getText()
						.toString());
			} catch (Exception e) {
				wcount = 0;
				e.printStackTrace();

			}
			String remark = et_activeapply_remark.getText().toString();
			// sendReplyText = Base64.encodeToString(sendReplyText.getBytes(),
			// Base64.NO_WRAP);

			String param = "{\"method\":\"activeSignUp\",\"userId\":\""
					+ application.userId + "\",\"activeId\":\"" + activeId
					+ "\",\"mcount\":\"" + mcount + "\",\"wcount\":\"" + wcount
					+ "\",\"remark\":\"" + remark + "\"}";
			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Log.i("kevinlog", param);
			Log.i("kevinlog", replyList);
			return replyList;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Log.i("kevinlog", result);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				String resultType = jsonObject.getString("status");
				if ("0".equals(resultType)) {
					CustomToast.CustomActivityToast(ActiveApplyActivity.this,
							"活动报名成功", 0);
					isJoin = 1;
					backIntent();
					// ActiveApplyActivity.this.finish();
				} else if ("100".equals(resultType)) {
					CustomToast.CustomActivityToast(ActiveApplyActivity.this,
							"服务器异常，请稍后重试", 0);
				} else if ("200".equals(resultType)) {
					CustomToast.CustomActivityToast(ActiveApplyActivity.this,
							"报名人数超出上限，无法报名", 0);
				} else if ("300".equals(resultType)) {
					CustomToast.CustomActivityToast(ActiveApplyActivity.this,
							"已报名该活动，无需重新报名", 0);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.i("kevinlog", "error");
				e.printStackTrace();
			}

			dialog.dismiss();
		}

	}
	
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if(keyCode == KeyEvent.KEYCODE_BACK){  
        	backIntent();
        }  
        return false;  
    }  
	
	private void backIntent(){
		Intent intent = new Intent(ActiveApplyActivity.this,ActiveDetailActivity.class);
		intent.putExtra("isJoin", isJoin+"");
		setResult(RESULT_OK,intent);
		ActiveApplyActivity.this.finish();
	}
}